package com.iemes.controller.planner;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.ClientPlannerFormMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.mapper.order.MaterialMapper;
import com.iemes.mapper.order.OrderMapper;
import com.iemes.mapper.planner.ClientPlannerMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/planner/")
@Controller
public class ClientPlannerController extends BaseController{

	@Inject
	private OrderMapper orderMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private MaterialMapper materialMapper;
	@Inject
	private ClientPlannerMapper clientPlannerMapper;
	//查看订单计划
		@RequestMapping("examineMaterial")
		public String examineOrder(Model model){
			List<ClientFormMap> clientFormMaps=clientMapper.findByWhere(new ClientFormMap());
			model.addAttribute("client", clientFormMaps);
			List<UserFormMap> userFormMaps=userMapper.findByWhere(new UserFormMap());
			model.addAttribute("alluser", userFormMaps);
			model.addAttribute("res", findByRes());
			return Common.BACKGROUND_PATH + "/planner/planner_list";
		}
		
		//计划分页
		@ResponseBody
		@RequestMapping("planner_findByPage")
		public PageView findByPage(String pageNow, String pageSize) 
		{
			ClientPlannerFormMap clientPlannerFormMap=getFormMap(ClientPlannerFormMap.class);
			List<Map<String, Object>> clientPlannerFormMaps=clientPlannerMapper.getAll(clientPlannerFormMap);
			try {
				LuogicPaging(pageNow, pageSize, clientPlannerFormMaps);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return pageView;
		}
	
	//新增
		@RequestMapping("order_planner")
		public String editUI(Model model) throws Exception 
		{
			String id = getPara("id");
			//获取所有客户
			List<ClientFormMap> clientFormMap=clientMapper.findByWhere(new ClientFormMap());
			model.addAttribute("client", clientFormMap);
			List<UserFormMap> userFormMaps=userMapper.findByWhere(new UserFormMap());
			model.addAttribute("alluser", userFormMaps);
			if(Common.isNotEmpty(id))
			{
				ClientPlannerFormMap materialFormMap=getFormMap(ClientPlannerFormMap.class);
				materialFormMap.put("order_id", id);
				List<ClientPlannerFormMap> materialFormMaps=materialMapper.findByWhere(materialFormMap);
				model.addAttribute("material", materialFormMaps);
				OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id, OrderFormMap.class);
				List<OrderFormMap> orderclient = orderMapper.getAllName();
				for (int i = 0; i < orderclient.size(); i++) {
					if(orderFormMap.getInt("client_no")==orderclient.get(i).getInt("client_no")){
						orderFormMap.put("clientname", orderclient.get(i).getStr("clientname"));
					}
					if(orderFormMap.getInt("salesman_id")==orderclient.get(i).getInt("salesman_id")){
						orderFormMap.put("salesman", orderclient.get(i).getStr("salesman"));
					}
					if(orderFormMap.getInt("planner_id")==orderclient.get(i).getInt("planner_id")){
						orderFormMap.put("planner", orderclient.get(i).getStr("planner"));
					}
				}
				
				model.addAttribute("order", orderFormMap);
			}
			
			return Common.BACKGROUND_PATH + "/order/order_planner";
		}
}
